package com.example.shw

import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.PixelFormat
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.MediaController
import android.widget.VideoView
import java.io.File

class VideoActivity : AppCompatActivity(){
    private lateinit var videoView : VideoView
    private lateinit var mController : MediaController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setFormat(PixelFormat.TRANSLUCENT)
        supportActionBar!!.hide()
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.clearFlags(WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
        window.navigationBarColor = Color.TRANSPARENT
        val intuiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION ; View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        ; View.SYSTEM_UI_FLAG_FULLSCREEN
        window.decorView.systemUiVisibility = intuiOptions
        //setFullScreen(false)
        setContentView(R.layout.activity_videoview)
        requestPermissions(arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE), 0x123)
    }
    private fun setFullScreen(enable : Boolean) {
        if (enable) {
            val attrs = window.attributes
            attrs.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN
            window.attributes = attrs
        } else {
            val attrs = window.attributes
            attrs.flags = WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
            window.attributes = attrs
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 0x123 && grantResults != null
            && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            videoView = findViewById(R.id.video)
            mController = MediaController(this)
            val video = File("/mnt/sdcard/test.mp4")
            if (video.exists()){
                videoView.setVideoPath(video.absolutePath)
                videoView.setMediaController(mController)
                mController.setMediaPlayer(videoView)
                videoView.requestFocus()
            }
        }
    }
}